// 捕获路由错误
const catchRouteErr = (err: any) => {
  if (
    // 忽略关于导航到它们已经在的页面的 vuex 错误。
    err.name !== "NavigationDuplicated" &&
    !err.message.includes("Avoided redundant navigation to current location")
  ) {
    // 但是将任何其他错误打印到控制台
    console.log(err);
  }
};

// 去除字符串中的空格
const removeBlank = (str: string): string => {
  return str.replace(/\s+/g, "");
};

// 校验对象身上是否有对应的key
const isValidKey = (
  key: string | number | symbol,
  object: object
): key is keyof typeof object => {
  return key in object;
};

// 校验是否是日期格式
const validateDate = function (date: string) {
  if (typeof date !== "string") {
    return false;
  }
  const dateReg = /^[0-9]{4,}-(0?[[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
  if (dateReg.test(date)) {
    return true;
  } else {
    return false;
  }
};

// 校验是否是24小时制时间格式
const validateTime = function (time: string) {
  if (typeof time !== "string") {
    return false;
  }
  const timeReg = /^(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
  if (timeReg.test(time)) {
    return true;
  } else {
    return false;
  }
};

// 以分隔符返回格式为YYYY DD MM字符串类型的当前日期
const getDate = function (separator: string) {
  const date = new Date();
  const year = date.getFullYear();
  let month: number | string = date.getMonth() + 1;
  if (month < 10) {
    month = `0${month}`;
  }
  let days: number | string = date.getDate();
  if (days < 10) {
    days = `0${days}`;
  }
  return [year, month, days].join(separator);
};

// 以分隔符返回格式为YYYY DD MM字符串类型的当前时间
const getTime = function (separator: string) {
  const date = new Date();
  const hours = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
  const minutes =
    date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
  const seconds =
    date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
  return [hours, minutes, seconds].join(separator);
};

// Pick对象上的指定键值对
const Omit = function <T extends Record<string, any>, P extends keyof T>(
  obj: T,
  args: P[]
) {
  const result = {} as Omit<T, P>;
  const keys = Object.keys(obj).filter(
    (item) => !args.includes(item as P)
  ) as Exclude<keyof T, P>[];
  keys.forEach((key) => {
    if (obj[key] !== undefined) result[key] = obj[key];
  });
  return result;
};

// 选择对象上的指定键值对
const Pick = function <T extends Record<string, any>, P extends keyof T>(
  obj: T,
  args: P[]
) {
  const result = {} as Pick<T, P>;
  const keys = Object.keys(obj).filter((item) =>
    args.includes(item as P)
  ) as Extract<keyof T, P>[];
  keys.forEach((key) => {
    if (obj[key] !== undefined) result[key] = obj[key];
  });
  return result;
};

// 读取文件数据生成DataURL
const readerFile = function (file: File) {
  return new Promise<FileReader>((res) => {
    const reader = new FileReader();

    reader.onload = function () {
      res(this);
    };

    reader.readAsDataURL(file);
  });
};

// 获取当前月份英文
function getCurrentEnMonth() {
  const month: Record<string, string> = {
    Jan: "January",
    Feb: "February",
    Mar: "March",
    Apr: "April",
    May: "May",
    Jun: "June",
    Jul: "July",
    Aug: "August",
    Sept: "September",
    Oct: "October",
    Nov: "November",
    Dec: "December",
  };
  const shortMonth = new Date().toDateString().split(" ")[1];
  return month[shortMonth];
}

export {
  catchRouteErr,
  removeBlank,
  isValidKey,
  validateDate,
  validateTime,
  getDate,
  getTime,
  Omit,
  Pick,
  readerFile,
  getCurrentEnMonth,
};
